#include <glib.h>

#define TEST_KEY_FILE_BASIC_FILE "./keyfile_remove.ini"

gint main(gint argc, gchar *argv) 
{
    gboolean ret = FALSE;
    GKeyFile *key_file = NULL;

    gchar *contents = NULL;
    gsize length = 0;

    gboolean bools[2] = { TRUE, FALSE };

    g_file_get_contents(TEST_KEY_FILE_BASIC_FILE, &contents, &length, NULL);
    g_print("before set:\n%s\n", contents);
    g_free(contents);

    key_file = g_key_file_new ();
    if(NULL == key_file) {
        g_print("key file new failed! \n");
    }

    ret = g_key_file_load_from_file(key_file, TEST_KEY_FILE_BASIC_FILE, 0, NULL);
    if(FALSE == ret) {
        g_print("key file load failed \n");
        return -1;
    }

    /* set value */
    g_print("******************** \n");
    g_print("set value \n");
    g_key_file_set_value(key_file, "Another Group", "New Item", "abcdefg");

    /* set group */
    g_print("******************** \n");
    g_print("set group and bool list \n");
    g_key_file_set_boolean_list(key_file, "New Group", "Bools", bools, 2);

    /* remove key */
    g_print("******************** \n");
    g_print("remove key \n");
    ret = g_key_file_remove_key(key_file, "Third Group", "AAA", NULL);
    g_print("remove key %s \n", ret?"Success":"Failed");

    /* remove group */
    g_print("******************** \n");
    g_print("remove group \n");
    ret = g_key_file_remove_group(key_file, "Fourth Group", NULL);
    g_print("remove group %s \n", ret?"Success":"Failed");
    
    g_key_file_save_to_file(key_file, TEST_KEY_FILE_BASIC_FILE, NULL);

    g_key_file_free(key_file);

    g_file_get_contents(TEST_KEY_FILE_BASIC_FILE, &contents, &length, NULL);
    g_print("after set and remove:\n%s\n", contents);
    g_free(contents);

    return 0;
}
